package cn.hzc.fortess.system.service;

import cn.hzc.fortess.core.common.annotion.CURDLog;
import cn.hzc.fortess.core.common.annotion.CURDType;
import cn.hzc.fortess.core.shiro.ShiroKit;
import cn.hzc.fortess.system.dao.LesseeDao;
import cn.hzc.fortess.system.entity.Lessee;
import cn.hzc.fortess.system.page.Pagination;
import cn.hzc.fortess.system.search.LesseeSearch;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class LesseeServiceImpl implements LesseeService {

    @Autowired
    LesseeDao lesseeDao;
    @Autowired
    DeptService deptService;

    @Autowired
    UserService userService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CURDLog(value = "新增租户")
    public void add(Lessee entity) {
        entity.setCreateTime(new Date());
        entity.setCreateUser(ShiroKit.getUser().getAccount());
        lesseeDao.insert(entity);
        //关联的项目，用","号分隔
        if (entity.getProjects() != null
                && StringUtils.isNotBlank(entity.getProjects().toString())) {
            for(String projectId:entity.getProjects().toString().split(",")){
                lesseeDao.saveRefProject(Integer.parseInt(projectId), entity.getId());
            }
        }
        //初始化该租户的部门结构，建立一个根结点
        deptService.createDeptRootByLessee(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CURDLog(value = "编辑租户",curdType = CURDType.EDIT)
    public void update(Lessee entity) {
        entity.setUpdateTime(new Date());
        entity.setUpdateUser(ShiroKit.getUser().getAccount());
        lesseeDao.update(entity);
        //关联的项目，用","号分隔
        lesseeDao.clearRefProject(entity.getId());
        if (entity.getProjects() != null
                && StringUtils.isNotBlank(entity.getProjects().toString())) {
            for (String projectId : entity.getProjects().toString().split(",")) {
                lesseeDao.saveRefProject(Integer.parseInt(projectId), entity.getId());
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CURDLog(value = "删除租户",curdType = CURDType.DELETE)
    public void delete(Integer id) {
        lesseeDao.delete(id);
        lesseeDao.deleteRelationByLessee(id);
        Integer rootDeptId = deptService.getRootDeptByLessee(id);
        if (rootDeptId != null) {
            deptService.delete(rootDeptId);
        }
    }

    @Override
    public Lessee getById(Integer id) {
        return lesseeDao.findById(id);
    }

    @Override
    public List<Integer> getRefProjectByLesseeId(Integer id) {
        return lesseeDao.getRefProjectByLesseeId(id);
    }

    @Override
    public List<Lessee> getByProjectId(Integer projectId) {
        List<Lessee> lessees = lesseeDao.getByProjectId(projectId);
        if(lessees==null){
            lessees = Lists.newArrayList();
        }
        return lessees;
    }

    @Override
    public Pagination getPage(LesseeSearch search) {
        Pagination pagination = new Pagination();
        pagination.setCount(lesseeDao.count(search));
        if (pagination.getCount() > 0) {
            List<Lessee> lessees = lesseeDao.page(search);
            lessees.parallelStream().forEach(lessee -> {
                List<String> projects = lesseeDao.getRefProjectNamesByLesseeId(lessee.getId());
                lessee.setProjects(CollectionUtils.isEmpty(projects) ? Lists.newArrayList() : projects); //租户关联的项目
            });
            pagination.setData(lessees);
        }
        return pagination;
    }
}
